package com.axis2.common.util;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.InputTag;
import org.htmlparser.util.NodeList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author yemulin
 * @create 2012-9-4 上午11:46:57
 */
public class HtmlUtil {

	public static Map<String, String> parseParam(String htmtText) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		try {
			NodeFilter filter = new NodeClassFilter(InputTag.class);
			Parser parser = new Parser();
			parser.setInputHTML(htmtText);
			parser.setEncoding(parser.getEncoding());
			NodeList list = parser.extractAllNodesThatMatch(filter);
			for (int i = 0; i < list.size(); i++) {
				InputTag node = (InputTag) list.elementAt(i);
				String name = node.getAttribute("name");
				String value = node.getAttribute("value");
				map.put(name, value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	public static String getHtmlNodeText(String html, String node) {
		Document doc = Jsoup.parse(html);
		Elements ele = doc.select(node);
		return ele.text();
	}

	public static void main(String[] args) {
		String html = "<html><head><title>qwqweqw</title><result>0</result></head><body>1234</body></html>";
		System.out.println(getHtmlNodeText(html, "body"));
	}
}
